<template>
	<view class="fill-question-wrap">

		<div class="question-list-wrap">
			<span>问题分类</span>


			<u-radio-group v-model="radiovalue1" placement="row" @change="groupChange">
				<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item, index) in radiolist1" :key="index"
					:label="item.name" :name="item.name" @change="radioChange">
				</u-radio>
			</u-radio-group>
		</div>


		<!-- 		<u-radio-group v-model="value" placement="row">
			<u-radio activeColor="red" label="123"></u-radio>
			<u-radio activeColor="red" label="456"></u-radio>
			<u-radio activeColor="red" label="789"></u-radio>
		</u-radio-group> -->
		<u--textarea v-model="value1" placeholder="请输入内容" count height="300"></u--textarea>

		<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
			:maxCount="10"></u-upload>

		<u-button type="primary" text="发起提问" @click="addProblem"></u-button>
		<u-toast ref="uToast"></u-toast>


	</view>
</template>
<script>
	import {
		categoryAPI,
		addProblemAPI,
		uploadAPI
	} from '../../../api/info.js'

	export default {
		data() {
			return {
				// 基本案列数据
				radiolist1: [{
						name: '问题一',
						disabled: false
					},
					{
						name: '问题二',
						disabled: false
					},
					{
						name: '问题三',
						disabled: false
					},
					{
						name: '问题四',
						disabled: false
					},
					{
						name: '问题五',
						disabled: false
					},
					{
						name: '问题六',
						disabled: false
					},
					{
						name: '问题七',
						disabled: false
					},
					{
						name: '问题八',
						disabled: false
					}
				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				radiovalue1: '苹果',
				value1: '',
				fileList1: [],
			};
		},
		mounted() {
			this.getCategory();
		},
		methods: {
			groupChange(n) {
				console.log('groupChange', n);
			},
			radioChange(n) {
				console.log('radioChange', n);
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://39.105.98.166:8008/api/api/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log("上传成功")
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			getCategory() {
				categoryAPI().then(res => {
					console.log(res);
				})
			},

			addProblem() {
				let params = {
					"magUrl": "",
					"msgText": "",
					"questionType": 0
				}

				addProblemAPI(params).then(res => {
					console.log(res);
					this.$refs.uToast.show({
						type: 'success',
						title: '成功主题(带图标)',
						message: "提交成功",
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
					})
				})
			}
		}
	};
</script>


<style lang="scss">
	.fill-question-wrap {
		background-color: white;
		height: 100vh;
	}

	.question-list-wrap {
		width: 100%;

		::v-deep .u-radio-group--row {
			flex-wrap: wrap;
		}
	}
</style>